package io.renren.modules.alarm.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.modules.alarm.dao.AlarmTacticsDao;
import io.renren.modules.alarm.entity.AlarmTacticsEntity;
import io.renren.modules.alarm.entity.AlarmTacticsSensorEntity;
import io.renren.modules.alarm.service.AlarmTacticsService;
import io.renren.modules.collecting.dao.CollectingDeviceDao;
import io.renren.modules.scheduled.MyTask;
import io.renren.modules.scheduled.ScheduleTask;
import io.renren.modules.scheduled.ScheduleUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class AlarmTacticsServiceImpl implements AlarmTacticsService {

    @Resource
    private AlarmTacticsDao alarmTacticsDao;
    @Resource
    private CollectingDeviceDao collectingDeviceDao;

    //获取报警策略集合
    //Integer pageNum, Integer pageSize, String name, String healthOrComfort
    @Override
    public PageUtils getList(Map<String, Object> params) {
        IPage<AlarmTacticsEntity> page = alarmTacticsDao.getList(new Query<AlarmTacticsEntity>().getPage(params), params);
        List<AlarmTacticsEntity> records = page.getRecords();
        for (AlarmTacticsEntity alarmTactics : records) {
            alarmTactics.setAlarmTacticsSensor(alarmTacticsDao.getSensorList(alarmTactics.getId()));
        }
        return new PageUtils(page);
    }

    //获取报警策略详情
    @Override
    public R get(Integer id) {
        AlarmTacticsEntity alarmTactics = alarmTacticsDao.get(id);
        if (null != alarmTactics && !"".equals(alarmTactics)) {
            alarmTactics.setAlarmTacticsSensor(alarmTacticsDao.getSensorList(id));
        }
        return R.ok(alarmTactics);
    }

    //报警策略新增
    @Override
    @Transactional
    public R add(AlarmTacticsEntity alarmTactics) {
        //新增报警策略
        alarmTacticsDao.add(alarmTactics);

        Set<Integer> set = new HashSet();

        List<AlarmTacticsSensorEntity> alarmTacticsSensor = alarmTactics.getAlarmTacticsSensor();

        BigDecimal b = new BigDecimal("100");//限值起点
        if (null != alarmTacticsSensor && alarmTacticsSensor.size() > 0) {
            //新增报警策略关联传感器
            for (AlarmTacticsSensorEntity sensor : alarmTacticsSensor) {
                //报警id
                sensor.setTacticsId(alarmTactics.getId());
                //限值上下限处理
                if (null != sensor.getLowe() && !"".equals(sensor.getLowe())) {
                    sensor.setLowe(new BigDecimal(sensor.getLowe()).divide(b).toString());
                }
                if (null != sensor.getUpper() && !"".equals(sensor.getUpper())) {
                    sensor.setUpper(new BigDecimal(sensor.getUpper()).divide(b).toString());
                }
                alarmTacticsDao.addSensorList(sensor);
                set.add(sensor.getDeviceId());
            }
            //修改设备状态
            collectingDeviceDao.updateState("1",set);
        }
        //判断报警策略是否开启
        if ("开启".equals(alarmTactics.getState())) {
            //新建执行任务
            ScheduleTask helloTask1 = new MyTask(alarmTactics.getId());
            //调用定时任务方法
            ScheduleUtil.start1(helloTask1, Long.valueOf(alarmTactics.getFrequency()) * 1000);
        }
        return R.ok("新增成功");
    }

    @Override
    public R update(AlarmTacticsEntity alarmTactics) {
        //修改报警策略
        alarmTacticsDao.update(alarmTactics);

        List<AlarmTacticsSensorEntity> alarmTacticsSensor = alarmTactics.getAlarmTacticsSensor();
        //修改报警策略关联传感器
        //无法新增或删除关联传感器，只能修改数值
        for (AlarmTacticsSensorEntity sensor : alarmTacticsSensor) {
            alarmTacticsDao.updateSensorList(sensor);
        }

        //新建执行任务
        ScheduleTask helloTask1 = new MyTask(alarmTactics.getId());
        //判断报警策略是否开启
        if ("开启".equals(alarmTactics.getState())) {
            //调用定时任务方法
            ScheduleUtil.start1(helloTask1, Long.valueOf(alarmTactics.getFrequency()) * 1000);
        } else {
            //停止定时任务
            ScheduleUtil.cancel(helloTask1);
        }
        return R.ok("修改成功");
    }

    @Override
    public R delete(Integer id) {
        //删除报警策略
        alarmTacticsDao.delete(id);
        //删除关联传感器集合
        alarmTacticsDao.deleteSensorList(id);
        return R.ok("删除成功");
    }

    @Override
    public R deleteSensor(Integer sensorId) {
        //删除单个关联传感器
        alarmTacticsDao.deleteSensor(sensorId);
        return R.ok("删除成功");
    }

    @Override
    public void getRunList() {
        List<AlarmTacticsEntity> runList = alarmTacticsDao.getRunList();
        for (AlarmTacticsEntity alarmTactics : runList) {
            //新建执行任务
            ScheduleTask helloTask1 = new MyTask(alarmTactics.getId());
            //调用定时任务方法
            ScheduleUtil.start1(helloTask1, Long.valueOf(alarmTactics.getFrequency()) * 1000);
        }
    }

}
